{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'weights': [{'data': [-0.54190427, -0.27866048, 0.455306, -0.77466439, 0.2155413, 0.63149892, 0.96253877, -0.87251032, 0.5999195, -0.80610289, -0.1982645, 0.32431534, 0.93117182, -0.03819988, -0.47177543, 0.17483424, -0.88284286, 0.19139394, -0.11495341, 0.06681537, 0.18449563, -0.18105407, 0.40700154, -0.92213003, -0.79312868, -0.43548578, -0.6937702, -0.39989327, -0.36228429, 0.39306052, 0.35325382, 0.88492784, -0.18250706, 0.16155788, 0.41390947, -0.78237669, -0.20556843, -0.31064771, 0.25995609, -0.26086483, -0.68690492, -0.84234127, 0.71760244, 0.82241492, 0.66498028, 0.24531482, -0.42529677, -0.1975344, 0.2370744, 0.56347711, 0.82975085, 0.79694468, 0.2928859, -0.22128013, 0.71509939, -0.51856729, -0.06366519, 0.72865484, 0.19756596, 0.93603065, -0.15084021, -0.1689197, 0.41645923, 0.4026665, 0.80837102, -0.3004439, -0.19871903, -0.21682387, -0.38842743, -0.57839535, -0.49843779, 0.21023487, 0.90348714, -0.75704365, 0.00040865, 0.26400099, -0.23104133, -0.94006091, -0.50783639, 0.54894291, 0.31426992, -0.2139014, 0.78043251, 0.853875, -0.91062654, 0.07838259, -0.02629358, 0.47074804, -0.19907572, -0.59608873], 'shape': [3, 3, 2, 5]}, {'data': [-0.61153601, 0.8694064, 0.28018421, 0.96263283, -0.07187857], 'shape': [5]}, {'data': [0.23551283, -0.39464683, 0.89320993, 0.93499946, 0.84763587], 'shape': [5]}, {'data': [0.70368475, -0.90025953, 0.88006859, 0.19645696, 0.12316286], 'shape': [5]}, {'data': [0.56451316, 0.49527774, 0.83890439, -0.10189393, 0.53392238], 'shape': [5]}, {'data': [0.54476614, 0.43296596, 0.82355662, 0.81937529, 0.95590748], 'shape': [5]}, {'data': [-0.64757194, 0.38294579, 0.15387812, 0.90138681, -0.53161741, 0.35252906, -0.02235672, -0.74986305, -0.04463964, 0.00454036, 0.87915417, -0.60734393, 0.96179323, 0.53666761, 0.38496633, 0.42331201, 0.02650542, 0.23362457, -0.24138609, -0.91613239], 'shape': [1, 1, 5, 4]}, {'data': [-0.51744242, 0.26675251, -0.91537145, 0.3509806], 'shape': [4]}, {'data': [-0.49133238, 0.53946673, 0.32629449, -0.5869313], 'shape': [4]}, {'data': [0.52385359, 0.30660211, 0.31233849, 0.06620905], 'shape': [4]}, {'data': [-0.77285789, -0.8460116, -0.4997778, -0.61713712], 'shape': [4]}, {'data': [0.44486243, 0.62358341, 0.51217101, 0.77369451], 'shape': [4]}, {'data': [-0.26641783, 0.21101274, 0.10673114, -0.26512734, -0.88191077, 0.37535685, -0.97515663, -0.73215051, 0.98281271, 0.99204448, 0.96142256, 0.84381878, 0.02804255, 0.95206406, -0.15328345, 0.81950569, 0.28767033, -0.58071021, 0.49915272, -0.25508646, -0.4838326, -0.2001564, 0.20669987, -0.25822963, 0.90178846, -0.06853458, -0.72876868, -0.00192717, 0.4961056, -0.26408008, -0.88339506, -0.05085536, -0.08630077, 0.27701807, 0.67914649, -0.06848802, -0.81702191, 0.20299124, -0.43500192, 0.8438674, 0.93241573, 0.95279356, -0.65085876, -0.96303719, -0.65858238, -0.21449723, 0.98544923, 0.10489501, -0.46444878, 0.28525886, -0.28180049, 0.40566621, -0.09303628, 0.14394578, 0.46452957, -0.12513119, -0.49020586, 0.54100835, 0.98308434, 0.38479304, -0.61824068, -0.20460531, 0.6388524, 0.98037162, -0.9818702, 0.38908975, 0.56118427, 0.88646173, 0.24810736, 0.35984305, 0.10004167, 0.09153771, -0.37469135, 0.32099458, -0.54337686, -0.03246755, 0.16232401, 0.265073, 0.33472883, -0.50945459, -0.34869639, 0.48172934, 0.50818247, 0.65720596, 0.83050092, -0.10554667, 0.46860173, 0.29619646, 0.17816559, 0.38350462, -0.26129366, -0.93324284, 0.76302869, 0.08332493, -0.54487301, -0.34188816, -0.50811034, -0.05639039, 0.50213215, -0.04448456, -0.07471556, 0.27643016, -0.15145411, 0.22111294, 0.49173953, -0.19818168, 0.27799311, 0.27739911], 'shape': [3, 3, 4, 3]}, {'data': [-0.11340936, -0.91676683, -0.5651004], 'shape': [3]}, {'data': [-0.65488319, 0.4099804, 0.32291475], 'shape': [3]}, {'data': [-0.93498039, 0.68023768, -0.62056578], 'shape': [3]}, {'data': [0.86320517, -0.79710709, 0.30719735], 'shape': [3]}, {'data': [0.78552591, 0.98972743, 0.06610293], 'shape': [3]}, {'data': [-0.90788009, -0.65871158, 0.98369049, 0.29383902, -0.08742277, 0.69663703, 0.82887138, 0.70554946, -0.14470764, 0.13519366, 0.04637206, -0.24907638, 0.19448248, 0.37161779, 0.56028265, 0.49605271, 0.32952396, 0.50606391, -0.94529562, -0.32078199, 0.3111684, 0.98133456, 0.04259265, 0.25723684, 0.08302491, 0.35536265, 0.42758731, -0.67743478, 0.53619969, 0.46189744, -0.03201824, -0.27080139, -0.49775568, 0.29504415, -0.43338293, -0.85852925, -0.57121818, 0.15370162, 0.88746426, -0.82947518, -0.29624711, 0.13686893, 0.05752348, 0.2162744, -0.82797366, -0.61618495, 0.06020317, -0.23374197, 0.13961779, -0.0900274, -0.3206224, 0.87718281, -0.32669526, -0.4710945], 'shape': [3, 3, 3, 2]}, {'data': [0.0231515, -0.51293283], 'shape': [2]}, {'data': [0.13848836, -0.35128712], 'shape': [2]}, {'data': [0.37373003, 0.90556202], 'shape': [2]}, {'data': [0.28104076, -0.95338109], 'shape': [2]}, {'data': [0.13453168, 0.10767889], 'shape': [2]}], 'expected': {'data': [0.81196368, -0.11035025, 0.62276578, -0.11035025, 2.22645187, -0.11035025, 2.66768837, -1.83787632, 0.26800883, -0.11035025, 1.67517114, -0.11035025, 2.20183444, -0.8188796, 0.26800883, -1.61873186, 1.85180569, -1.5101192], 'shape': [3, 3, 2]}, 'input': {'data': [-0.54190427, -0.27866048, 0.455306, -0.77466439, 0.2155413, 0.63149892, 0.96253877, -0.87251032, 0.5999195, -0.80610289, -0.1982645, 0.32431534, 0.93117182, -0.03819988, -0.47177543, 0.17483424, -0.88284286, 0.19139394, -0.11495341, 0.06681537, 0.18449563, -0.18105407, 0.40700154, -0.92213003, -0.79312868, -0.43548578, -0.6937702, -0.39989327, -0.36228429, 0.39306052, 0.35325382, 0.88492784, -0.18250706, 0.16155788, 0.41390947, -0.78237669, -0.20556843, -0.31064771, 0.25995609, -0.26086483, -0.68690492, -0.84234127, 0.71760244, 0.82241492, 0.66498028, 0.24531482, -0.42529677, -0.1975344, 0.2370744, 0.56347711, 0.82975085, 0.79694468, 0.2928859, -0.22128013, 0.71509939, -0.51856729, -0.06366519, 0.72865484, 0.19756596, 0.93603065, -0.15084021, -0.1689197, 0.41645923, 0.4026665, 0.80837102, -0.3004439, -0.19871903, -0.21682387, -0.38842743, -0.57839535, -0.49843779, 0.21023487, 0.90348714, -0.75704365, 0.00040865, 0.26400099, -0.23104133, -0.94006091, -0.50783639, 0.54894291, 0.31426992, -0.2139014, 0.78043251, 0.853875, -0.91062654, 0.07838259, -0.02629358, 0.47074804, -0.19907572, -0.59608873, 0.77239477, 0.54773798, 0.00922646, -0.44019973, 0.81720055, -0.0615295, 0.04580207, -0.76165178, -0.25095654, -0.24994101, 0.45502047, -0.75264239, -0.69142981, 0.02687807, 0.32093283, 0.88250988, 0.61121992, -0.50937295, 0.77718591, 0.40262635, -0.62736296, -0.29367364, -0.36348673, 0.63311157, 0.83600435, -0.90951031, -0.32951743, 0.54277901, 0.24301942, 0.03862923, 0.16270639, 0.48954823, -0.57044853, -0.33256914, -0.78071628, -0.07926009, 0.23073969, -0.51236684, 0.48137712, 0.76199354, 0.07620622, 0.34468054, 0.88032903, 0.85625296, 0.42121203, 0.04009794, 0.79783, 0.7082213, 0.1576071, -0.00959212, 0.61794887, 0.22218222, -0.95200956, -0.83814455, -0.97645341, -0.79525945, 0.23180734, -0.39176507, -0.00617481, -0.35796406, -0.94958437, 0.49854253, 0.35452684, 0.83471916, 0.35123934, 0.6688845, 0.69015915, 0.68934495, -0.24558832, 0.85902393, 0.88134197, -0.47357725], 'shape': [9, 9, 2]}}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (9, 9, 2)\n",
    "\n",
    "conv_0 = Conv2D(5, 3, 3, activation='relu', border_mode='same', subsample=(2, 2), dim_ordering='tf', bias=True)\n",
    "bn_0 = BatchNormalization(mode=0, axis=-1, epsilon=1e-3)\n",
    "conv_1 = Conv2D(4, 1, 1, activation='linear', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "bn_1 = BatchNormalization(mode=0, axis=-1, epsilon=1e-3)\n",
    "conv_2 = Conv2D(3, 3, 3, activation='relu', border_mode='same', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "bn_2 = BatchNormalization(mode=0, axis=-1, epsilon=1e-3)\n",
    "conv_3 = Conv2D(2, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "bn_3 = BatchNormalization(mode=0, axis=-1, epsilon=1e-3)\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "x = bn_0(x)\n",
    "x = conv_1(x)\n",
    "x = bn_1(x)\n",
    "x = conv_2(x)\n",
    "x = bn_2(x)\n",
    "x = conv_3(x)\n",
    "output_layer = bn_3(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(5000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(5000 + i)\n",
    "    if i % 6 == 5:\n",
    "        # std should be positive\n",
    "        weights.append(np.random.random(w.shape))\n",
    "    else:\n",
    "        weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
